package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidLog;
import com.apache.bid.manager.BidLogManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidLogManagerImpl implements BidLogManager {

	protected IDao bidLogDao;
	protected final String entityName = "com.apache.bid.entity.BidLog";

	public void setBidLogDao(IDao bidLogDao) {
		this.bidLogDao = bidLogDao;
	}

	public String saveInfo(ParamsVo<BidLog> vo) throws BusinessException {
		BidLog bidLog = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidLog_"+infoId;
		bidLog.setLogId(infoId);
		MethodParam param = new MethodParam("BidLog", cacheKey, "", entityName);
		param.setVaule(bidLog);
		if (bidLogDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidLog> vo) throws BusinessException {
		BidLog bidLog = vo.getObj();
		if (Validator.isNotNull(bidLog.getLogId())) {
			String cacheKey = "";
			//String cacheKey="bidLog_"+bidLog.getLogid();
			MethodParam param = new MethodParam("BidLog", cacheKey, "", entityName);
			param.setVaule(bidLog);
			return bidLogDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidLog> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidLog_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidLog info = (BidLog) bidLogDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidLog");
			return bidLogDao.edit(param);
		} else {
			param.setParams("logId", infoId);
			param.setDelete(true);
			return bidLogDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidLog> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidLog_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidLogDao.selectById(param);
	}

	public Object execute(ParamsVo<BidLog> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidLog> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidLogDao.pageSelect(param);
		return page;
	}

	public List<BidLog> getList(ParamsVo<BidLog> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidLogDao.select(param);
	}

	public long countInfo(ParamsVo<BidLog> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidLogDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidLog> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidLogDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidLog bidLog = vo.getObj();
		param.setResultCache(false);
		if (Validator.isNotNull(bidLog.getLogId())) {
			sb.append(" and logId = :logId");
			param.setParams("logId", bidLog.getLogId());
		}
		if (Validator.isNotNull(bidLog.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidLog.getSessionId());
		}
		if (Validator.isNotNull(bidLog.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", bidLog.getUserId());
		}
		if (Validator.isNotNull(bidLog.getLogName())) {
			sb.append(" and logName = :logName");
			param.setParams("logName", bidLog.getLogName());
		}
		if (Validator.isNotNull(bidLog.getLogContent())) {
			sb.append(" and logContent = :logContent");
			param.setParams("logContent", bidLog.getLogContent());
		}
		if (Validator.isNotNull(bidLog.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidLog.getCreateTime());
		}
		if (Validator.isNotNull(bidLog.getWhoUserName())) {
			sb.append(" and whoUserName = :whoUserName");
			param.setParams("whoUserName", bidLog.getWhoUserName());
		}
		if (Validator.isNotNull(bidLog.getOrgCname())) {
			sb.append(" and orgCname = :orgCname");
			param.setParams("orgCname", bidLog.getOrgCname());
		}
		if (Validator.isNotNull(bidLog.getObjectType())) {
			sb.append(" and objectType = :objectType");
			param.setParams("objectType", bidLog.getObjectType());
		}
		if (Validator.isNotNull(bidLog.getObjectId())) {
			sb.append(" and objectId = :objectId");
			param.setParams("objectId", bidLog.getObjectId());
		}
		if (Validator.isNotNull(bidLog.getObjectName())) {
			sb.append(" and objectName = :objectName");
			param.setParams("objectName", bidLog.getObjectName());
		}
		if (Validator.isNotNull(bidLog.getIp())) {
			sb.append(" and ip = :ip");
			param.setParams("ip", bidLog.getIp());
		}
		if (Validator.isNotNull(bidLog.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidLog.getSpare1());
		}
		if (Validator.isNotNull(bidLog.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidLog.getSpare2());
		}
		if (Validator.isNotNull(bidLog.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidLog.getSpare3());
		}
		if (Validator.isNotNull(bidLog.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidLog.getSpare4());
		}
		if (Validator.isNotNull(bidLog.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidLog.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
